#include "../test.h"
#include "../ds_types.h"
#include "../sq_list/sq_list.h"
#include <stdlib.h>
#define MM 100000000

size_t search_1(sq_list *s,const void * e, int compare(const void *, const void*), size_t siz)
{
	size_t i;
	sqlist_insert(s, sqlist_length(s) + 1, e, siz);
	for( i = 1; compare(sqlist_getelem(s, i, siz), e); i++);
	if( i == sqlist_length(s)) return 0;
	return i;
}
size_t search(sq_list *s,const void * e, int compare(const void *, const void*), size_t siz)
{
	size_t i;
	for(i = 1; i <= sqlist_length(s); i++)
		if(!compare(sqlist_getelem(s, i, siz), e))
			return i;
	return 0;
}
size_t search_b(sq_list *s,const void * e, int compare(const void *, const void*), size_t siz)
{
	size_t low = 1, high = sqlist_length(s), mid = (low + high)/2;
	int r;
	while(low <= high){
		r = compare(sqlist_getelem(s, mid, siz), e);
		if(r == 0) return mid;
		if(r > 0) high = mid - 1;
		else low = mid + 1;
		mid = (low + high) / 2;
	}
	return 0;
}
int main()
{
	size_t siz = sizeof(struct mydata);
	size_t i;
	struct mydata e1;
	sq_list s;
	sqlist_init(&s, siz);
	for(i = 1; i <= MM; i++){
		e1.value = i;
		e1.ch = 'a';
		sqlist_insert(&s, i, (void *)&e1, siz);
	}
	//sqlist_traverse(&s, visit,siz);
	visit(sqlist_getelem(&s, MM,siz));
	printf("999\n");
	e1.value = 2123570486;
	e1.value = MM;
	printf("%d\n", search_b(&s , (void *)&e1, my_compare,siz));
}

